<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="商品名称" prop="mc">
        <el-input
          v-model="queryParams.mc"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['shop:sp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:sp:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:sp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['shop:sp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品id" align="center" prop="sId" />
      <el-table-column label="分类id" align="center" prop="typeId" />
      <el-table-column label="订单id" align="center" prop="ddId" />
      <el-table-column label="标签id" align="center" prop="bqId" />
      <el-table-column label="商品名称" align="center" prop="mc" />
      <el-table-column label="商品价格" align="center" prop="jd" />
      <el-table-column label="商品库存" align="center" prop="kc" />
      <el-table-column label="商品简介" align="center" prop="jj" />
      <el-table-column label="发货地址" align="center" prop="address" />
      <el-table-column label="保质期" align="center" prop="bzq" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.bzq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产日期" align="center" prop="scrq" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.scrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="折扣" align="center" prop="zk" />
      <el-table-column label="店铺名称" align="center" prop="dp" />
      <el-table-column label="包装" align="center" prop="bz" />
      <el-table-column label="品牌" align="center" prop="pp" />
      <el-table-column label="储存条件" align="center" prop="tj" />
      <el-table-column label="材质" align="center" prop="cz" />
      <el-table-column label="颜色" align="center" prop="ys" />
      <el-table-column label="使用年龄" align="center" prop="age" />
      <el-table-column label="许可证编号" align="center" prop="xkz" />
      <el-table-column label="售后电话" align="center" prop="sh" />
      <el-table-column label="商品图片" align="center" prop="img" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['shop:sp:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['shop:sp:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="spRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类id" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入分类id" />
        </el-form-item>
        <el-form-item label="订单id" prop="ddId">
          <el-input v-model="form.ddId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="标签id" prop="bqId">
          <el-input v-model="form.bqId" placeholder="请输入标签id" />
        </el-form-item>
        <el-form-item label="商品名称" prop="mc">
          <el-input v-model="form.mc" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品价格" prop="jd">
          <el-input v-model="form.jd" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="商品库存" prop="kc">
          <el-input v-model="form.kc" placeholder="请输入商品库存" />
        </el-form-item>
        <el-form-item label="商品简介" prop="jj">
          <el-input v-model="form.jj" placeholder="请输入商品简介" />
        </el-form-item>
        <el-form-item label="发货地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="保质期" prop="bzq">
          <el-date-picker clearable
            v-model="form.bzq"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择保质期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生产日期" prop="scrq">
          <el-date-picker clearable
            v-model="form.scrq"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="折扣" prop="zk">
          <el-input v-model="form.zk" placeholder="请输入折扣" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="dp">
          <el-input v-model="form.dp" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="包装" prop="bz">
          <el-input v-model="form.bz" placeholder="请输入包装" />
        </el-form-item>
        <el-form-item label="品牌" prop="pp">
          <el-input v-model="form.pp" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="储存条件" prop="tj">
          <el-input v-model="form.tj" placeholder="请输入储存条件" />
        </el-form-item>
        <el-form-item label="材质" prop="cz">
          <el-input v-model="form.cz" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="颜色" prop="ys">
          <el-input v-model="form.ys" placeholder="请输入颜色" />
        </el-form-item>
        <el-form-item label="使用年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入使用年龄" />
        </el-form-item>
        <el-form-item label="许可证编号" prop="xkz">
          <el-input v-model="form.xkz" placeholder="请输入许可证编号" />
        </el-form-item>
        <el-form-item label="售后电话" prop="sh">
          <el-input v-model="form.sh" placeholder="请输入售后电话" />
        </el-form-item>
        <el-form-item label="商品图片" prop="img">
          <el-input v-model="form.img" placeholder="请输入商品图片" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Sp">
import { listSp, getSp, delSp, addSp, updateSp } from "@/api/shop/sp"

const { proxy } = getCurrentInstance()

const spList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sId: null,
    typeId: null,
    ddId: null,
    bqId: null,
    mc: null,
    jd: null,
    kc: null,
    jj: null,
    address: null,
    bzq: null,
    scrq: null,
    zk: null,
    dp: null,
    bz: null,
    pp: null,
    tj: null,
    cz: null,
    ys: null,
    age: null,
    xkz: null,
    sh: null,
    img: null
  },
  rules: {
    typeId: [
      { required: true, message: "分类id不能为空", trigger: "blur" }
    ],
    mc: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    jd: [
      { required: true, message: "商品价格不能为空", trigger: "blur" }
    ],
    kc: [
      { required: true, message: "商品库存不能为空", trigger: "blur" }
    ],
    jj: [
      { required: true, message: "商品简介不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "发货地址不能为空", trigger: "blur" }
    ],
    bzq: [
      { required: true, message: "保质期不能为空", trigger: "blur" }
    ],
    scrq: [
      { required: true, message: "生产日期不能为空", trigger: "blur" }
    ],
    zk: [
      { required: true, message: "折扣不能为空", trigger: "blur" }
    ],
    dp: [
      { required: true, message: "店铺名称不能为空", trigger: "blur" }
    ],
    bz: [
      { required: true, message: "包装不能为空", trigger: "blur" }
    ],
    pp: [
      { required: true, message: "品牌不能为空", trigger: "blur" }
    ],
    tj: [
      { required: true, message: "储存条件不能为空", trigger: "blur" }
    ],
    cz: [
      { required: true, message: "材质不能为空", trigger: "blur" }
    ],
    ys: [
      { required: true, message: "颜色不能为空", trigger: "blur" }
    ],
    age: [
      { required: true, message: "使用年龄不能为空", trigger: "blur" }
    ],
    xkz: [
      { required: true, message: "许可证编号不能为空", trigger: "blur" }
    ],
    sh: [
      { required: true, message: "售后电话不能为空", trigger: "blur" }
    ],
    img: [
      { required: true, message: "商品图片不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询商品列表 */
function getList() {
  loading.value = true
  listSp(queryParams.value).then(response => {
    spList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    sId: null,
    typeId: null,
    ddId: null,
    bqId: null,
    mc: null,
    jd: null,
    kc: null,
    jj: null,
    address: null,
    bzq: null,
    scrq: null,
    zk: null,
    dp: null,
    bz: null,
    pp: null,
    tj: null,
    cz: null,
    ys: null,
    age: null,
    xkz: null,
    sh: null,
    img: null
  }
  proxy.resetForm("spRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.sId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加商品"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _sId = row.sId || ids.value
  getSp(_sId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改商品"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["spRef"].validate(valid => {
    if (valid) {
      if (form.value.sId != null) {
        updateSp(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addSp(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _sIds = row.sId || ids.value
  proxy.$modal.confirm('是否确认删除商品编号为"' + _sIds + '"的数据项？').then(function() {
    return delSp(_sIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('shop/sp/export', {
    ...queryParams.value
  }, `sp_${new Date().getTime()}.xlsx`)
}

getList()
</script>
